package Service_Impl;

import java.util.List;

import Dao.IBanjiDao;
import Dao_Impl.BanjiDaoImpl;
import Entity.PageBean;
import JavaServlet.TimeServlet.Banji;
import Service.IBanjiService;

public class BanjiServiceImpl implements IBanjiService{

	private IBanjiDao banjiDao = new BanjiDaoImpl();
	@Override
	public List<Banji> selectAll() {
		// 体现Service业务逻辑，给所有班级名字前面加上"刘老师"
		List<Banji>list = banjiDao.selectAll();
		for(Banji banji : list) {
			String name = banji.getName();
			banji.setName("刘老师--" + name);
		}
		return list;
	}


	@Override
	public boolean insert(Banji banji) {
		// 1、数据库中查看是不是有该姓名的学生 selectByName(name)
		// 2.1、如果有：表示该名字用户已经存在,不去调用insert
		// 2.2、如果没有：表示该名字用户不存在,去调用insert插入这个用户
		List<Banji> list =  banjiDao.selectByName(banji.getName());
		if(list.size() > 0) {
			System.out.println("该用户已存在");
			return true;
		}else {
			int count = banjiDao.insert(banji);
			return count == 1 ? true : false;
		}
	}

	@Override
	public boolean update(Banji banji) {
		int count = banjiDao.update(banji);
		return count == 1 ? true : false;
	}

	@Override
	public boolean deleteById(Integer id) {
		// 业务逻辑：id在10-100之间的不删除
		int count = banjiDao.deleteById(id);
		return count == 1 ? true : false;
	}
	
	@Override
	public List<Banji> searchName(String name){
		return banjiDao.searchName(name);
	}
	
	@Override
	public PageBean pageList(Integer pageNo, Integer pageSize){
		PageBean pageBean = new PageBean();
		// 当前是第几页
		pageBean.setPageNo(pageNo);
		// 一页有多少条数据
		pageBean.setPageSize(pageSize);
		// 一共有多少页
		int count = banjiDao.getTotalCount();
		int totalPage = (int)Math.ceil((double)count / pageSize);
		pageBean.setTotalPage(totalPage);
		
		// 当前页数据
		int offset = (pageNo - 1) * pageSize;
		List<Banji>list = banjiDao.pageList(offset,pageSize);
		pageBean.setList(list);
		return pageBean;
	}


	@Override
	public Banji selectById(int id) {
		return banjiDao.selectById(id);
	}
}
